package com.jy.servlet;

import com.alibaba.fastjson.JSON;
import com.jy.service.ShoppingTrolleyService;
import com.jy.service.impl.ShoppingTrolleySericelmpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class cart_DeleteServlet extends HttpServlet {
    private static final Logger log = LoggerFactory.getLogger(cartServlet.class);
    private ShoppingTrolleyService shoppingTrolleyServlet;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String productName = request.getParameter("productName");
        String admin = request.getParameter("name");
        shoppingTrolleyServlet = new ShoppingTrolleySericelmpl();

        //判断该商品的购物车记录是否删除了
        Boolean favg = shoppingTrolleyServlet.deleteByName(productName);

        //查询该账号的购物车数量，并给到session里面去
        HttpSession session = request.getSession();
        int Shopping_count = shoppingTrolleyServlet.selectByName(admin);
        session.setAttribute("Shopping_count",Shopping_count);

        String data = JSON.toJSONString(favg);

        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        out.println(data);
        log.debug("data:" +data);


    }
}
